package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;

import edu.pku.sei.modeler.uml.ResourceFactory;
import edu.pku.sei.modeler.uml.test.FragmentBorder;

/**
 * @author zsj
 * 
 */
public class InteractionUseFigure extends CommonFigure {
	private Label ref;

	private Label text;
	
	public InteractionUseFigure() {
		setLayoutManager(new XYLayout());
		setBorder(new FragmentBorder());
		setBackgroundColor(ColorConstants.listBackground);
		setOpaque(true);

		ref = new Label("ref");
		ref.setForegroundColor(ColorConstants.black);
		ref.setFont(ResourceFactory.getHeadFont());
		text = new Label("N");
		text.setForegroundColor(ColorConstants.black);
		text.setFont(new Font(Display.getDefault(), "Arial", 16, SWT.BOLD));
		add(ref);
		add(text);
	}

	public void changeLineColor(Color color) {
		((FragmentBorder)this.getBorder()).setColor(color);
//		this.setForegroundColor(color);
	}
	
	
//	 public Rectangle getPreferredBounds(Rectangle suggestedBounds) {
//		 int width = selectPreferred(MIN_WIDTH, DEFAULT_WIDTH,
//		 suggestedBounds.width);
//		 int height = selectPreferred(MIN_HEIGHT, DEFAULT_HEIGHT,
//		 suggestedBounds.height);
//		 return new Rectangle(suggestedBounds.x, suggestedBounds.y, width,
//		 height);
//	 }
	//
	// private int selectPreferred(int min, int defaultvalue, int suggested) {
	// return suggested == 0 ? defaultvalue : max(min, suggested);
	// }

//	@Override
//	// TODO Remove - this should be handled by layout handler
//	public Dimension getPreferredSize(int w, int h) {
//		Dimension prefSize = super.getPreferredSize(w, h);
//		Dimension defaultSize = new Dimension(200, 100);
//		prefSize.union(defaultSize);
//		return prefSize;
//	}

//	@Override
//	protected void paintFigure(Graphics graphics) {
//		/**
//		 * titleFlow.setFont(ResourceFactory.getHeadFont());
//		titleFlow.setForegroundColor(ColorConstants.black);
//		Rectangle rect = getBounds().getCopy();
//		rect.crop(new Insets(2, 0, 2, 0));
//		graphics.setBackgroundColor(FILL_COLOR);
//		graphics.setForegroundColor(this.getForegroundColor());
//		graphics.fillRectangle(rect);
//		((FragmentBorder) getBorder()).setBackgroundColor(this.getBackgroundColor());
//		((FragmentBorder) getBorder()).setColor(this.getForegroundColor());
//		((FragmentBorder) getBorder()).paintLabelBackground(this, graphics);
//		 */
//		
//		
//		//super.paintFigure(graphics);
//		ref.setForegroundColor(ColorConstants.black);
//		ref.setFont(this.getFont());
//		Rectangle rect = getBounds().getCopy();
//		rect.crop(new Insets(2, 0, 2, 0));
//	//	graphics.setBackgroundColor(FILL_COLOR);
//		graphics.setForegroundColor(this.getForegroundColor());
//		graphics.fillRectangle(rect);
//		((FragmentBorder) getBorder()).setBackgroundColor(this
//				.getBackgroundColor());
//		// ((FragmentBorder) getBorder()).setColor(this.getBorderColor());
//		//((FragmentBorder) getBorder()).paintLabelBackground(this, graphics);
//	}

	@Override
	public void validate() {
		if (isValid())
			return;
		super.validate();

		Rectangle tr = ref.getTextBounds();
		ref.setSize(tr.width, tr.height);

		final Rectangle r = getBounds();
		tr = text.getTextBounds();
		text.setLocation(new Point((r.width - tr.width) / 2,
				(r.height - tr.height) / 2));
		text.setSize(tr.width, tr.height);
	}

	// TODO Pull up to superclass
	@Override
	protected boolean useLocalCoordinates() {
		return true;
	}

	// Called by the FragmentBorder to paint a frame label
	// border of the correct size
	public Rectangle getLocalTextBounds() {
		Dimension extent = FigureUtilities.getTextExtents(ref.getText(), ref
				.getFont());
		return new Rectangle(ref.getLocation(), extent);
	}

	// --- ILabeledFigure ---

	public Rectangle getLabelBounds() {
		Rectangle rect = text.getClientArea().getCopy();
		text.translateToAbsolute(rect);
		return rect;
	}

	public void setLabel(String labelText) {
		text.setText(labelText);
		repaint();
	}
	
	
	
	
	public Label getNameLabel() {
		return this.text;
	}
}
